<template>

  <div class="container">

    <el-container>
      <el-header>

        <el-row>
          <el-col :span="12">
            <div class="grid-content bg-purple text_left">Header</div>
          </el-col>
          <el-col
            :span="12"
            class="text_right"
          >
            <el-button
              type="info"
              plain
              @click="backLogin"
            >退出</el-button>
          </el-col>
        </el-row>

      </el-header>
      <el-container>
        <el-aside :width=" iscollapse ? '64px' : '200px' ">
          <div
            @click="toggleMenu"
            class="toggle-menu"
          >|||</div>
          <!-- <h5>默认颜色</h5> -->
          <el-menu
            :default-active="activePath"
            class="el-menu-vertical-demo"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#409eff"
            unique-opened
            router
            :collapse="iscollapse"
            :collapse-transition="false"
          >
            <!-- 一级菜单 -->
            <el-submenu
              :index="item.id+''"
              v-for="(item,index) in menus"
              :key="index"
            >
              <template slot="title">
                <i :class="iconsObj[item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item
                :index="'/'+e.path+''"
                v-for="(e,i) in item.children"
                :key="i"
                @click="saveNavSatus('/'+e.path)"
              >
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{ e.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>

          </el-menu>

        </el-aside>
        <el-container>
          <el-main>
            <router-view></router-view>
          </el-main>
          <!-- <el-footer>Footer</el-footer> -->
        </el-container>
      </el-container>
    </el-container>

  </div>

</template>

<script>
export default {
  name: '',
  data() {
    return {
      menus: [],
      iconsObj: {
        '125': 'iconfont icon-users',
        '103': 'iconfont icon-3702mima',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      iscollapse: false,
      activePath: ''
    }
  },
  created() {
    this.getMenus()
    this.activePath = window.sessionStorage.getItem('activePath')
    console.log(this.activePath)
  },
  methods: {
    backLogin() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenus() {
      // 获取 token
      let token = window.sessionStorage.getItem('token')
      // console.log(token)
      let { data: res } = await this.$axios.get('menus', {
        headers: {
          'Authorization': token
        }
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // console.log(res.data)
      this.menus = res.data
      // console.log(res.meta.msg)
    },
    // 切换左侧菜单
    toggleMenu() {
      this.iscollapse = !this.iscollapse
    },
    // 保存链接的激活状态
    saveNavSatus(activePath) {
      console.log(activePath)
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }

}
</script>

<style lang="scss" scoped>
.container {
  height: 100%;
  .el-container {
    height: 100%;
  }
  .el-main {
    text-align: left;
    line-height: 2;
  }
  .el-menu {
    border: none;
  }
  .el-aside {
    text-align: left;
  }
}
.el-header,
.el-footer {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #545c64;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
.toggle-menu {
  color: #fff;
  background-color: #606266;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  letter-spacing: 2px;
  font-weight: bold;
  cursor: pointer;
}
.iconfont {
  margin-right: 5px;
}
</style>